package dao;

import com.alibaba.fastjson.JSONObject;
import pojo.*;
import jdbc.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class schooltreasure_teacherDaoImp implements schooltreasure_teacherDao {

    @Override
    public int addSchooltreasure_teacher(teacher teacher) throws SQLException {

        jdbc jdbc = new jdbc();
        Connection connection = jdbc.lianjie();
        String sql = "insert into schooltreasure.teacher(teacherName, userName, password,level ) values ('" + teacher.getTeacherName() + "', '" + teacher.getUserName() + "', '" + teacher.getPassword() + "', '" + teacher.getLevel() + "')";
        PreparedStatement Statement = connection.prepareStatement(sql);
        int i = Statement.executeUpdate();
        jdbc.guanbi(null, Statement, connection);
        return i;
    }


    @Override
    public int denglu(String userName, int password) throws SQLException {
        jdbc jdbc = new jdbc();
        Connection connection = jdbc.lianjie();
        String sql = "SELECT * FROM schooltreasure.teacher where userName='" + userName + "' and password='" + password + "' ";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        int x = -1;
        while (resultSet.next()) {

            x = resultSet.getInt("level");
        }
        jdbc.guanbi(resultSet, preparedStatement, connection);
        return x;


    }

    @Override
    public int selectUserName(String userName) throws SQLException, IOException, ClassNotFoundException {

        jdbc jdbc = new jdbc();
        Connection connection = jdbc.lianjie();
        String sql = "SELECT count(*) as sl FROM schooltreasure.teacher where userName='" + userName + "'";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        int i = 0;
        while (resultSet.next()) {
            i = resultSet.getInt("sl");
        }
        jdbc.guanbi(resultSet, preparedStatement, connection);
        return i;
    }


}




